<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'student';

    public function plan()
    {
        return $this->belongsTo(Plan::class, 'control_plan', 'id');
    }

    public function project()
    {
        return $this->belongsTo(Project::class, 'today_plan', 'id');
    }

    public function invite()
    {
        return $this->belongsTo(InviteLog::class, 'id', 'student_id');
    }

    public static function addInvite($phone, $age, $begin_control)
    {
        $s = new Student();
        $s->name = $phone;
        if (!is_numeric($age)) {
            $age = 0;
        }
        $s->birthday = date('Y-m-d', strtotime('- ' . $age . ' years'));
        $s->phone = $phone;
        $s->sex = 0;
        $s->begin_control = $begin_control;
        $s->after_control = 0;
        $s->control_plan = 0;
        $s->control_result = 0;
        $s->today_plan = 0;
        $s->member_id = date('YmdHis');
        $s->card_id = '-';
        $s->money = 0;
        $s->score = 0;
        $s->status = 0;
        $s->type = 0;
        $s->alias = '-';
        $s->save();

        return $s->id ?? 0;
    }
}
